package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShareIntents {
    private Context context;
    private Intent intent;

    private ShareIntents( Context context)
    {
        this.context = context;
    }

    public static ShareIntents from( Context context)
    {
        return new ShareIntents(context);
    }

    public ShareIntents shareText(String subject, String message)
    {
        intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.setParam("android.intent.extra.SUBJECT", subject);
        intent.setParam("android.intent.extra.TEXT", message);
        intent.setType("text/plain");
        return this;
    }

    public ShareIntents shareText(String subject, String message, String chooserDialogTitle)
    {
        Intent shareIntent = new Intent();
        shareIntent.setAction("android.intent.action.SEND");
        shareIntent.setParam("android.intent.extra.TEXT", message);
        shareIntent.setParam("android.intent.extra.SUBJECT", subject);
        shareIntent.setType("text/*");
        //Intent.createChooser API need to be found
       // intent = Intent.createChooser(shareIntent, chooserDialogTitle);
        return this;
    }

    // TODO:
    private ShareIntents shareImage()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.setParam("android.intent.extra.STREAM", "/sdcard/1App/1.jpeg");
        intent.setType("image/jpeg");
        return this;
    }

    // TODO:
    private ShareIntents shareMultipleFiles()
    {
        List<String> paths = new ArrayList<>();
        paths.add("Storage/emulated/0/1App/Peak Sports Rule Bazaar 1.2");
        paths.add("Storage/emulated/0/1App/Peak Sports Rule Myket 1.2");
        paths.add("Storage/emulated/0/1App/Peak Sports Rule IranApps 1.2");
        if (paths.size() == 0)
        {
            return null;
        }
        ArrayList<Uri> uris = new ArrayList<>();
        intent = new Intent();
        intent.setAction("android.intent.action.SEND_MULTIPLE");
        intent.setType("*/*");
        for (String path : paths)
        {
            File file = new File(path);
            uris.add(Uri.getUriFromFile(file));
        }
        intent.setParcelableArrayListParam("android.intent.extra.STREAM", uris);

        return this;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
